package com.example.yuanann.vegetable_app.repository;

import android.content.Context;

import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.example.yuanann.vegetable_app.repository.dao.UserDao;
import com.example.yuanann.vegetable_app.repository.entity.User;

@Database(entities = {User.class}, version = 1, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    public abstract UserDao userDao();

    private static volatile AppDatabase INSTANCE;

    /**
     * 获取数据库
     * @param context
     * @return
     */
    public static AppDatabase getDatabase(final Context context) {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                                    AppDatabase.class, "shopping_app_database")
                            .fallbackToDestructiveMigration() // 数据库升级策略
                            .build();
                }
            }
        }
        return INSTANCE;
    }
}
